package cn.tedu.epidemic.webapi.repo.impl;

import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeListItemVO;
import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeStandardVO;
import cn.tedu.community.commons.pojo.notice.vo.UserNoticeListItemVO;
import cn.tedu.community.commons.pojo.notice.vo.UserNoticeStandardVO;
import cn.tedu.epidemic.webapi.repo.INoticeRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Repository
public class NoticeRedisRepositoryImpl implements INoticeRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void saveOne(AdmNoticeStandardVO admNoticeStandardVO) {
        String key = ADM_NOTICE_ITEM_KEY_PREFIX + admNoticeStandardVO.getId();
        ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, admNoticeStandardVO);
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(NOTICE_ALL_KEYS_KEY, key);
    }

    @Override
    public void save(UserNoticeStandardVO userNoticeStandardVO) {
        String key = USER_NOTICE_ITEM_KEY_PREFIX + userNoticeStandardVO.getId();
        ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, userNoticeStandardVO);
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(NOTICE_ALL_KEYS_KEY, key);
    }

    @Override
    public void saveAdmNoticeList(List<AdmNoticeListItemVO> admNoticeListItemVO) {
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        for (AdmNoticeListItemVO admNoticeListItem : admNoticeListItemVO) {
            opsForList.rightPush(ADM_NOTICE_LIST_KEY, admNoticeListItem);
        }
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(NOTICE_ALL_KEYS_KEY, ADM_NOTICE_LIST_KEY);
    }

    @Override
    public void saveUserNoticeList(List<UserNoticeListItemVO> userNoticeListItemVO) {
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        for (UserNoticeListItemVO userNoticeListItem : userNoticeListItemVO) {
            opsForList.rightPush(USER_NOTICE_LIST_KEY, userNoticeListItem);
        }
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        opsForSet.add(NOTICE_ALL_KEYS_KEY, USER_NOTICE_LIST_KEY);
    }

    @Override
    public void deleteALl() {
        SetOperations<String, Serializable> opsForSet = redisTemplate.opsForSet();
        Set<Serializable> serializableSet = opsForSet.members(NOTICE_ALL_KEYS_KEY);
        Set<String> keys = new HashSet<>();
        for (Serializable serializable : serializableSet) {
            keys.add((String) serializable);
        }
        redisTemplate.delete(keys);
        redisTemplate.delete(NOTICE_ALL_KEYS_KEY);
    }

    @Override
    public AdmNoticeStandardVO getOne(Long id) {
        String key = ADM_NOTICE_ITEM_KEY_PREFIX + id;
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(key);
        if (serializable != null) {
            return (AdmNoticeStandardVO) serializable;
        }
        return null;
    }

    @Override
    public UserNoticeStandardVO get(Long id) {
        String key = USER_NOTICE_ITEM_KEY_PREFIX + id;
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(key);
        if (serializable != null) {
            return (UserNoticeStandardVO) serializable;
        }
        return null;
    }

    @Override
    public List<AdmNoticeListItemVO> admNoticeList() {
        return admNoticeList(0,-1);
    }

    @Override
    public List<AdmNoticeListItemVO> admNoticeList(long start, long end) {
        ListOperations<String,Serializable> opsForList = redisTemplate.opsForList();
        List<Serializable> list = opsForList.range(ADM_NOTICE_LIST_KEY,start,end);
        List<AdmNoticeListItemVO> admNoticeListItemVOS = new ArrayList<>();
        for (Serializable serializable : list) {
            admNoticeListItemVOS.add((AdmNoticeListItemVO) serializable);
        }
        return admNoticeListItemVOS;
    }

    @Override
    public List<UserNoticeListItemVO> userNoticeList() {
        return userNoticeList(0,-1);
    }

    @Override
    public List<UserNoticeListItemVO> userNoticeList(long start, long end) {
        ListOperations<String,Serializable> opsForList = redisTemplate.opsForList();
        List<Serializable> list = opsForList.range(USER_NOTICE_LIST_KEY,start,end);
        List<UserNoticeListItemVO> userNoticeListItemVOS = new ArrayList<>();
        for (Serializable serializable : list) {
            userNoticeListItemVOS.add((UserNoticeListItemVO) serializable);
        }
        return userNoticeListItemVOS;
    }
}
